// Copyright (c) 2009 Hypirinha contributers
// This program is made available under the terms of the MIT License.
package org.hypirinha.html;

import org.junit.Test;import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.List;
import java.util.ArrayList;

public class LicencingTest {

    @Test
    public void allSourceFilesShouldHaveCorrectCopyrightNotice() throws Exception {
        List<File> sourceFiles = DirectoryUtils.recursiveList(new File("."), new FilenameFilter() {
            public boolean accept(File directory, String fileName) {
                return fileName.endsWith(".java");
            }
        });

        List<File> brokenFiles = new ArrayList<File>();
        for (File sourceFile : sourceFiles) {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(sourceFile));
            String line1 = bufferedReader.readLine();
            String line2 = bufferedReader.readLine();
            bufferedReader.close();

            boolean hasLicence = true;
            hasLicence &= line1.equals("// Copyright (c) 2009 Hypirinha contributers");
            hasLicence &= line2.equals("// This program is made available under the terms of the MIT License.");
            if (!hasLicence) {
                brokenFiles.add(sourceFile);
            }
        }

        String message = "The following files do not have the correct copyright notice :\n";
        for (File brokenFile : brokenFiles) {
            message += brokenFile + "\n";
        }
        assertTrue(message, brokenFiles.isEmpty());
    }
}
